package com.example.myapplication.Fragment;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.activity.LoadingActivity;
import com.example.myapplication.R;
import com.example.myapplication.adapter.recyclerViewAdapter.MineIndexAdapter;
import com.example.myapplication.api.MusicApiService;
import com.example.myapplication.entity.PlayList;
import com.example.myapplication.entity.TopPlayLists;
import com.example.myapplication.entity.UserPlayList;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MineIndexFragment extends Fragment {
    private TextView mine_name;
    private ImageView mine_img;
    private TextView mine_fans;
    private TextView attention;
    private RecyclerView mine_recyclerview;
    private TwinklingRefreshLayout refreshLayout;
    private MineIndexAdapter adapter;
    private List<PlayList> playlistBeanList = new ArrayList<>();
    private List<PlayList> topPlayLists ;
    private static int count = 1;

    public MineIndexFragment(){

    }

    public static MineIndexFragment newInstance(Bundle bundle) {
        MineIndexFragment fragment = new MineIndexFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mine, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        mine_name = view.findViewById(R.id.mine_name);
        mine_img = view.findViewById(R.id.mine_img);
        mine_fans = view.findViewById(R.id.mine_fans);
        attention = view.findViewById(R.id.attention);
        refreshLayout = view.findViewById(R.id.refreshLayout);
        mine_name.setText(LoadingActivity.profileBean.getNickname());
        mine_fans.setText("粉丝 "+LoadingActivity.profileBean.getFolloweds());
        attention.setText("关注 "+LoadingActivity.profileBean.getFollows()+" ");
        RoundedCorners roundedCorners= new RoundedCorners(30);
        RequestOptions options= RequestOptions
                .bitmapTransform(roundedCorners)
                .circleCrop();
        Glide.with(getContext()).load(LoadingActivity.profileBean.getAvatarUrl())
                .centerCrop().apply(options).into(mine_img);

        mine_recyclerview = view.findViewById(R.id.mine_recyclerview);
        RecyclerView.LayoutManager manager = new LinearLayoutManager(getContext());
        mine_recyclerview.setLayoutManager(manager);
        adapter = new MineIndexAdapter(getContext());
        mine_recyclerview.setAdapter(adapter);
        loadData(LoadingActivity.profileBean.getUserId());
        loadData1(0);
        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter(){
            @Override
            public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refreshLayout.finishRefreshing();
                    }
                },2000);
            }

            @Override
            public void onLoadMore(final TwinklingRefreshLayout refreshLayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if(count<5) {
                            Toast.makeText(getContext(), "加载中，请稍后..", Toast.LENGTH_SHORT).show();
                            loadData1(count * 20);
                        }
                        else{
                            Toast.makeText(getContext(), "暂时没有更多了", Toast.LENGTH_SHORT).show();
                        }
                        count++;
                        refreshLayout.finishLoadmore();
                    }
                },2000);
            }
        });
    }

    private void loadData(Long id){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MusicApiService service =  retrofit.create(MusicApiService.class);

        Call<UserPlayList> call = service.getUserPlayList(id);
        call.enqueue(new Callback<UserPlayList>() {
            @Override
            public void onResponse(Call<UserPlayList> call, Response<UserPlayList> response) {
                UserPlayList baseResponse = response.body();
                playlistBeanList = baseResponse.getPlaylist();
                adapter.setData(playlistBeanList);
            }

            @Override
            public void onFailure(Call<UserPlayList> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    private void loadData1(int offset){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MusicApiService service =  retrofit.create(MusicApiService.class);
        Call<TopPlayLists> call = service.getTopPlayList(offset);
        call.enqueue(new Callback<TopPlayLists>() {
            @Override
            public void onResponse(Call<TopPlayLists> call, Response<TopPlayLists> response) {
                TopPlayLists baseResponse = response.body();
                topPlayLists = baseResponse.getPlaylists();
                adapter.setData(topPlayLists);
            }

            @Override
            public void onFailure(Call<TopPlayLists> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
}
